/// @tags: String
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

namespace BlueQuantum {

int const N = 2e5 + 5, M = 26;

int pre[N], suf[N];
char s[N], t[N];
vector<int> vec[M];

inline int main() {
  int n, m, slen, tlen;
  cin >> n >> m >> s >> t;
  slen = strlen(s), tlen = strlen(t);
  for (int i = 0; i < slen; ++i) { vec[s[i] - 'a'].push_back(i); }
  pre[0] = *vec[t[0] - 'a'].begin();
  for (int i = 1; i < tlen; ++i) {
    pre[i] = *upper_bound(vec[t[i] - 'a'].begin(), vec[t[i] - 'a'].end(), pre[i - 1]);
  }
  for (int i = 0; i < M; ++i) {
    reverse(vec[i].begin(), vec[i].end());
    for (auto j = vec[i].begin(); j != vec[i].end(); ++j) { *j = slen - *j - 1; }
  }
  reverse(t, t + tlen);
  suf[0] = *vec[t[0] - 'a'].begin();
  for (int i = 1; i < tlen; ++i) {
    suf[i] = *upper_bound(vec[t[i] - 'a'].begin(), vec[t[i] - 'a'].end(), suf[i - 1]);
  }
  reverse(suf, suf + tlen);
  int ans = 0;
  for (int i = 1; i < tlen; ++i) { ans = max(ans, (slen - suf[i] - 1) - pre[i - 1]); }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("C Maximum width.in", "r", stdin);
  freopen("C Maximum width.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
